package com.tl.sboot.web;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tl.sboot.entity.Proxy;
import com.tl.sboot.entity.Services;
import com.tl.sboot.entity.User;
import com.tl.sboot.service.UserService;
@Controller//默认返回json格式
@EnableAutoConfiguration//Spring Boot会自动根据你jar包的依赖来自动配置项目
public class UserController {
	@Autowired
	UserService userService;
	
	/**
	 * 带参数测试
	 * @param myName
	 * @return
	 */
	@RequestMapping(value="/hello/{myName}")
	@ResponseBody
	String index(@PathVariable String myName){
		return "Hello "+myName+"!!!";
	}
	/**
	 * 跳转到新增用户
	 * @return
	 */
	@RequestMapping(value="/addUserJsp")
	String addUserJsp(){
		return "addUser";
	}
	
	/**
	 * 通过http请求获取用户信息
	 * @return
	 */
	@RequestMapping(value="/getUsers")
	String getUsers(Model model){
		System.out.println("user:"+userService);
		List<User> users = userService.getUserList();
		model.addAttribute("users", users);
		return "userList";
	}
	/**
	 * 删除用户
	 * @param userName
	 * @param admin
	 * @return
	 */
	@RequestMapping(value="/delUser/{userName}")
	String delUser(@PathVariable String userName){
		userService.delUser(userName);
		System.out.println("已经删除数据");
		return "redirect:/getUsers";
	}
	
	/**
	 * 添加用户
	 * @param userName
	 * @param admin
	 * @return
	 */
	@RequestMapping(value="/addUser")
	String addUser(String userName,boolean admin){
		userService.addUser(userName, admin);
		return "forward:getUsers";
	}
	/**
	 * 通过http请求获取用户信息
	 * @return
	 */
	@RequestMapping(value="/getServicesList")
	String getServicesList(Model model){
		List<Services> servicesList = userService.getServicesList();
		model.addAttribute("servicesList", servicesList);
		return "servicesList";
	}
	
	/**
	 * 通过http请求获取用户信息
	 * @return
	 */
	@RequestMapping(value="/getProxys")
	String getProxys(Model model){
		List<Proxy> proxys = userService.getProxys();
		model.addAttribute("proxys", proxys);
		return "proxyList";
	}
	
	/**
	 * 跳转到blocklyDemo页面
	 * @return
	 */
	@RequestMapping(value="/blocklyDemo")
	public String blocklyDemo(){
		return "blocklyDemo";
	}
	/**
	 * 跳转到blocklyDemo2页面
	 * @return
	 */
	@RequestMapping(value="/blocklyDemo2")
	public String blocklyDemo2(){
		return "blocklyDemo2";
	}
	/**
	 * 跳转到blocklyDemo2页面
	 * @return
	 */
	@RequestMapping(value="/blocklyDemo3")
	public String blocklyDemo3(){
		return "blocklyDemo3";
	}
	
}